<template>
	<view class="container">
		<TopBtn :title="'发起会诊'"></TopBtn>
		<view class="info">
			<view class="currency_card">
				<view class="currency_title mb_30">
					<text class="red">*</text>
					邀请医师 ({{ teamList.length }}/10)
				</view>
				<view class="currency_info doctorList">
					<view class="" v-for="n in teamList">
						<image :src="n.avatar" mode=""></image>
						<view class="name">
							{{ n.name }}
						</view>
						<view class="position">￥{{ n.consultationFee || '暂无' }}</view>
					</view>
					<!-- <view
					  class=""
					  @click="
					    goInfo('pages/CommonTools/SelectConsultationPersonnel/SelectConsultationPersonnel')
					  "
					> -->
					<view class="" @click="chenge()">
						<image src="@/static/ConsultationRoom/add.png" mode=""></image>
						<view class="name">选择人员</view>
					</view>
				</view>
			</view>
			<!-- <patientCard></patientCard> -->
			<view class="currency_card">
				<uni-forms ref="baseForm" :label-width="120" :modelValue="baseFormData">
					<uni-forms-item label="会诊患者：" required>
						<view v-if="id" class="inputBox flex-center">
							{{ patient.patientName }} {{ patient.patientSex == 1 ? '男' : '女' }}
							{{ patient.patientAge }}岁
						</view>
						<view class="inputBox flex-center" @click="goInfo('pages/ConsultationRoom/SelectPatient/SelectPatient')"
							v-else>
							<view class="" v-if="patient.name">
								{{ patient.name }} {{ patient.sex == 1 ? '男' : '女' }} {{ patient.age }}岁
							</view>
							<view class="" v-else>
								请选择
								<uni-icons type="right" size="16"></uni-icons>
							</view>
						</view>
					</uni-forms-item>
					<uni-forms-item label="会诊时间：" required>
						<uni-datetime-picker :hide-second="true" :clear-icon="false" return-type="timestamp" type="datetime"
							:border="false" v-model="baseFormData.consultationTime" />
					</uni-forms-item>
					<uni-forms-item label="会诊费用：" required>
						<view class="inputBox flex-center">￥{{ baseFormData.amount }}</view>
					</uni-forms-item>

					<uni-forms-item label="简要病史：" class="pb_1" required></uni-forms-item>
					<view class="mt_30">
						<textArea v-model="baseFormData.briefMedical" class="mttextArea" :maxNum="100"></textArea>
					</view>
					<uni-forms-item label="辅助检查结果：" class="pb_1" required></uni-forms-item>
					<view class="mt_30">
						<textArea v-model="baseFormData.auxiliaryResult" class="mttextArea" :maxNum="100"></textArea>
					</view>
					<uni-forms-item label="初步诊断：" class="pb_1" required></uni-forms-item>
					<view class="mt_30">
						<textArea v-model="baseFormData.primaryDiagnosis" class="mttextArea" :maxNum="100"></textArea>
					</view>
					<uni-forms-item label="邀请会诊目的：" class="pb_1" required></uni-forms-item>
					<view class="mt_30">
						<textArea v-model="baseFormData.invitationPurpose" class="mttextArea" :maxNum="100"></textArea>
					</view>
				</uni-forms>
			</view>
			<!-- <view class="currency_card">
				<view class="currency_title">
					病情资料
				</view>
				<DiseaseCondition></DiseaseCondition>
			</view> -->
		</view>
		<view class="kong"></view>
		<view class="btn" @click="createConsultationInfo()">发布</view>
	</view>
</template>

<script>
	import {
		SendGroupMessage
	} from '@/utils/TIM'
	import {
		createConsultationInfo,
		getConsultationDoctorInfo,
		getPatientInfoByOutId,
		createConsultinfoOrder,
	} from '@/api/ConsultationRoom'
	import {
		getPatientInfo
	} from '@/api/user'

	export default {
		data() {
			return {
				groupId: '',
				consultationID: undefined,
				baseFormData: {
					amount: 0,
				},
				ids: '',
				teamList: [],
				patient: {},
				userId: uni.getStorageSync('userId'),
				id: '',
			}
		},
		onShow() {
			let that = this
			uni.$off('ids')
			uni.$once('ids', function(data) {
				that.ids = that.userId + ',' + data
				that.baseFormData.amount = 0
				console.log(that.ids)
				that.getConsultationDoctorInfo()
			})
			uni.$off('patientId')
			uni.$once('patientId', function(data) {
				that.getPatientInfo(data)
			})
		},
		onUnload() {},
		onLoad(e) {
			if (e.id) {
				this.id = e.id
				this.groupId = e.groupId
				this.getPatientInfoByOutId()
			}
		},
		methods: {
			chenge() {
				var that = this
				uni.showActionSheet({
					itemList: ['团队医生', '平台医生'],
					success: function(res) {
						console.log(res)
						if (res.tapIndex == 0) {
							that.goInfo('pages/CommonTools/SelectConsultationPersonnel/SelectConsultationPersonnel')
						} else if (res.tapIndex == 1) {
							that.goInfo('pages/ConsultationRoom/SelectPersonnel/SelectPersonnel?type=group')
						}
					},
					fail: function(res) {
						console.log(res.errMsg)
					},
				})
			},
			getPatientInfoByOutId() {
				getPatientInfoByOutId({
					outpatientId: this.id,
				}).then(res => {
					this.patient = res.data
					this.baseFormData.patientId = res.data.patientId
				})
			},
			getPatientInfo(e) {
				getPatientInfo({
					id: e,
				}).then(res => {
					this.patient = res.data
					this.baseFormData.patientId = res.data.id
				})
			},
			getConsultationDoctorInfo() {
				getConsultationDoctorInfo({
					ids: this.ids,
				}).then(res => {
					this.teamList = res.data
					for (let i = 0; i < res.data.length; i++) {
						this.baseFormData.amount += res.data[i].consultationFee
					}
				})
			},
			createConsultationInfo() {
				if (!this.teamList.length > 0) {
					uni.showToast({
						title: '请选择医师',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.consultationTime) {
					uni.showToast({
						title: '请选择会诊时间',
						duration: 2000,
						icon: 'none',
					})
					return
				}

				if (!this.baseFormData.patientId) {
					uni.showToast({
						title: '请选择患者',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.briefMedical) {
					uni.showToast({
						title: '请填写简要病史',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.auxiliaryResult) {
					uni.showToast({
						title: '请填写辅助检查结果',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.primaryDiagnosis) {
					uni.showToast({
						title: '请填写初步诊断',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.invitationPurpose) {
					uni.showToast({
						title: '请填写邀请会诊目的',
						duration: 2000,
						icon: 'none',
					})
					return
				}

				if (!this.baseFormData.patientId) {
					uni.showToast({
						title: '请选择患者',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.briefMedical) {
					uni.showToast({
						title: '请填写简要病史',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.auxiliaryResult) {
					uni.showToast({
						title: '请填写辅助检查结果',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.primaryDiagnosis) {
					uni.showToast({
						title: '请填写初步诊断',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.invitationPurpose) {
					uni.showToast({
						title: '请填写邀请会诊目的',
						duration: 2000,
						icon: 'none',
					})
					return
				}

				this.baseFormData.doctorInfoRespVOS = this.teamList
				this.baseFormData.participants = this.ids

				createConsultationInfo(this.baseFormData).then(async res => {
					createConsultinfoOrder({
						consultinfoId: res.data,
					})

					// 发送群消息
					this.sendMessage()

					uni.navigateBack({
						delta: 1, //返回层数，2则上上页
					})
				})
			},
			goInfo(e, id) {
				if (id) {
					uni.navigateTo({
						url: `/${e}?id=${id}`,
					})
				} else {
					uni.navigateTo({
						url: `/${e}`,
					})
				}
			},
			sendMessage(text) {
				// 发送群消息
				const message = {
					businessID: 'tip',
					type: 'card',
					title: '会诊服务',
					text: `医生已为您推荐了会诊服务的医生，点击即可缴费`,
					buttonText: '去缴费',
					buttonUrl: {
						patient: `/pagesCommon/order/orderDetails?id=${this.orderId}`,
						doctor: ``,
					},
				}
				SendGroupMessage(this.groupId, message)
			},
		},
	}
</script>

<style lang="less">
	.kong {
		height: 150rpx;
	}

	/deep/.uni-date__x-input {
		text-align: right;
	}

	/deep/.uni-date-editor--x {
		.uni-icons {
			display: none;
		}
	}

	.inputBox {
		height: 100%;
		justify-content: right;
	}

	.btn {
		width: 649rpx;
		height: 100rpx;
		background: #3b6cfe;
		border-radius: 50rpx;
		font-weight: 500;
		font-size: 28rpx;
		color: #ffffff;
		text-align: center;
		line-height: 100rpx;
		position: fixed;
		left: 50%;
		bottom: 39rpx;
		transform: translateX(-50%);
	}

	.red {
		margin-right: 5rpx;
	}

	.mttextArea {
		margin-top: -30rpx;
	}

	/deep/.uni-forms-item {
		margin-bottom: 10rpx;
	}

	.greyBg {
		width: 649rpx;
		height: 172rpx;
		background: #fafafa;
		border-radius: 10rpx;
		font-weight: 500;
		font-size: 28rpx;
		color: #333333;
		padding: 29rpx 16rpx;
		box-sizing: border-box;
	}

	.info {
		padding: 30rpx;

		.doctorList {
			display: flex;
			flex-wrap: wrap;
			text-align: center;

			&>view {
				min-width: 20%;
				padding: 0 10rpx;
				box-sizing: border-box;
				margin-bottom: 20rpx;

				.name {
					font-weight: 500;
					font-size: 28rpx;
					color: #333333;
				}

				.position {
					font-weight: 500;
					font-size: 24rpx;
					color: red;
				}
			}

			image {
				width: 100rpx;
				height: 100rpx;
				border-radius: 50%;
			}
		}

		.currency_card {
			position: relative;

			.state {
				position: absolute;
				right: 0;
				top: 0;
				padding: 0 5rpx;
				min-width: 100rpx;
				height: 50rpx;
				background: #f9fbfe;
				font-size: 28rpx;
				color: #3e6efe;
				text-align: center;
				line-height: 50rpx;
				border-radius: 0 21rpx;
			}
		}

		&>view {
			margin-bottom: 27rpx;
		}

		.currency_info {
			margin-top: 21rpx;

			&:last-child {
				margin-top: 0rpx;
			}

			.Card_row {
				display: flex;
				font-weight: 500;
				font-size: 28rpx;
				color: #333;
				align-items: center;
				min-height: 50rpx;

				.title {
					width: 150rpx;
					color: #999;
				}
			}
		}
	}
</style>